<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Welcome extends CI_Controller {

	/**
	 * Index Page for this controller.
	 *
	 * Maps to the following URL
	 * 		http://example.com/index.php/welcome
	 *	- or -
	 * 		http://example.com/index.php/welcome/index
	 *	- or -
	 * Since this controller is set as the default controller in
	 * config/routes.php, it's displayed at http://example.com/
	 *
	 * So any other public methods not prefixed with an underscore will
	 * map to /index.php/welcome/<method_name>
	 * @see https://codeigniter.com/user_guide/general/urls.html
	 */
	 public function __construct() {
		parent::__construct();
		$this->load->library('session');
		$this->load->model('user_model');
		$this->load->helper('url');
	}
	public function index()
	{
		$this->load->view('templates/head');		
		$this->load->view('welcome_message');
		$this->load->view('templates/foot');
	}
	
	public function login() 
	{
		//接收POST信息，并与数据库中用户密码对照，如果正确信息将存入Session。
		$id = $this->input->post('id');
		$passwd = $this->input->post('passwd');
		
		$user = $this->user_model->get_by_id ($id);
		
		if (password_verify($passwd, $user['pass_wd'])) {
		
		$newdata = array (
			'user_id' => $user['id'],
			'user_name' => $user['name'],
			'user_position' => $user['position']		
		)	;
		
		$this->session->set_userdata($newdata);
		
		$data['user_name'] = $user['name'];
		//加载模版解析类
		$this->load->library('parser');
		$this->load->view('templates/head');
		$this->load->view('templates/menu');
		$this->parser->parse('success', $data);
		$this->load->view('templates/foot');
		} else {
			echo "<p class='text-center'>登录失败。</p>";		
		}
		
	}
	public function logout() {
		//删除Session并返回登录界面
		unset(
			$_SESSION['user_id'],
    		$_SESSION['user_name'],
    		$_SESSION['position']
		);
		
		$this->load->view('templates/head');		
		$this->load->view('welcome_message');
		$this->load->view('templates/foot');
	}
	public function ch_passwd () {
		isset($_SESSION['user_name']) OR exit('请先登录！');	
		$this->load->helper('form');
		$this->load->library('form_validation');
		
		$this->form_validation->set_rules('old_passwd', '旧密码', 'required');
		$this->form_validation->set_rules('new_passwd', '新密码', 'required');
		$this->form_validation->set_rules('re_passwd', '再次输入新密码', 'required|matches[new_passwd]', array(
			'required' => 'The %s filed is required.',
			'matches' => '两次输入的新密码不一致。'
			)
		);
		
		if ($this->form_validation->run() == FALSE)
        {
            $this->load->view('templates/head');
				$this->load->view('templates/menu');
				$this->load->view('form_ch_passwd');
				$this->load->view('templates/foot');
        }
        else
        {
            $user_id = $_SESSION['user_id'];
            $user = $this->user_model->get_by_id ($user_id);
            $old_passwd = $this->input->post('old_passwd');
            if (password_verify($old_passwd, $user['pass_wd'])) {
            	$this->user_model->ch_passwd($user_id);
            	$this->load->view('templates/head');
					$this->load->view('templates/menu');
					$this->load->view('ch_success');
					$this->load->view('templates/foot');
				} else {
				echo"<p class='text-center lead'>密码输入错误，请重新登录。</p>";
				$this->load->view('templates/head');		
				$this->load->view('welcome_message');
				$this->load->view('templates/foot');
				}
        }
		
	}
}